package com.sinosoft.jwjw.utils.generateCode;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

import java.util.Map;


import cn.hutool.core.util.StrUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.util.ResourceUtils;

public class FreeMarkerUtil {  
  
    private Configuration cfg;  
  
    public void init() throws Exception {
        // 创建一个Configuration实例  
        cfg = new Configuration(Configuration.VERSION_2_3_28);
        String path = "classpath:generatorTemp";
        // 设置FreeMarker的模版文件位置  
        cfg.setDirectoryForTemplateLoading(  ResourceUtils.getFile(path));
    }


    /**
     * 生成代码文件的方法
     * @param hf 模板
     * @param model_name 模块名称（model的名称）
     * @param model_desc 模块的描述
     * @param savePath 保存的路径
     * @throws Exception 抛出的异常信息
     */
    public void process(FreeMarkerUtil hf,String model_name,String model_desc,String savePath) throws Exception {
  
        Map<String,String> root = new HashMap<String,String>();
  
        root.put("model_name", model_name);
        root.put("model_desc",model_desc);
        root.put("model_name_lowerFirst", StrUtil.lowerFirst(model_name));
        //生成controller层
        String fileNameController = model_name + "Controller.java";
        Template templateController= cfg.getTemplate("TempCrudController.ftl");
        hf.buildTemplate(root, savePath, fileNameController, templateController);
        //生成service层

        String fileNameService = model_name + "Service.java";
        Template templateService= cfg.getTemplate("TempCrudService.ftl");
        hf.buildTemplate(root, savePath, fileNameService, templateService);
        //生成serviceImp层
        String fileNameServiceImp = model_name + "ServiceImp.java";
        Template templateServiceImp= cfg.getTemplate("TempCurdServiceImp.ftl");
        hf.buildTemplate(root, savePath, fileNameServiceImp, templateServiceImp);
    }  

    public void buildTemplate(Map root,  String savePath,
            String fileName, Template template) throws IOException {  
        String realFileName = savePath + fileName;
  

        System.out.println("---------生成路径------"+realFileName);
        File newsDir = new File(savePath);
        if (!newsDir.exists()) {  
            newsDir.mkdirs();  
        }  
        try {  
            Writer out = new OutputStreamWriter(new FileOutputStream(  
                    realFileName), StandardCharsets.UTF_8);
            System.out.println("------realFileName--------"+realFileName);
            template.process(root, out);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
    }  
  
    public static void main(String[] args) throws Exception {  
        FreeMarkerUtil hf = new FreeMarkerUtil();
        hf.init();  
        String savePath ="D:/test1/";
        hf.process(hf,"SUserTest","用户管理",savePath);
    }  
  
}  
